import QtQuick 2.15
import SddmComponents 2.0
import "logic.js" as Logic

Rectangle {
    id: root
    width:  Screen.width
    height: Screen.height

    Connections {
        target: sddm

        onLoginSucceeded: {
            errorMessage.color = config.green
            errorMessage.text = config.loginSucceeded
        }

        onLoginFailed: {
            errorMessage.color = config.red
            errorMessage.text = config.loginFailed
        }
    }

    Repeater {
        model: screenModel
        Background {
            x: geometry.x
            y: geometry.y
            width: geometry.width
            height: geometry.height
            source: Logic.getBg()
            fillMode: Image.Tile
            onStatusChanged: {
                if (status === Image.Error) {
                    source = config.defaultBg
                }
            }
        }
    }

    Rectangle {
        id: mainRect
        width: parent.width / 3
        color: config.charcoal
        radius: 5
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 32
        anchors.topMargin: 32
        anchors.leftMargin: 32

        Column {
            id: mainColumn
            anchors.centerIn: parent
            spacing: parent.width * 0.05
            width: parent.width * 0.9

            Row {
                id: greet
                width: parent.width
                anchors.verticalCenter: parent.verticalCenter
                anchors.verticalCenterOffset: -(mainRect.height * 0.45)

                Text {
                    width: parent.width
                    color: config.periwinkle
                    text: qsTr(config.greet)
                    font.pixelSize: 32
                    elide: Text.ElideMiddle
                }
            }

            Row {
                width: parent.width
                spacing: Math.round(parent.width * 0.06)
                anchors.verticalCenter: greet.verticalCenter
                anchors.verticalCenterOffset: (mainRect.height * 0.25)

                Text {
                    id: lblName
                    width: parent.width * 0.25
                    color: config.periwinkle
                    text: qsTr(config.userName)
                    font.pixelSize: 28
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideMiddle
                }

                TextBox {
                    id: name
                    width: parent.width * 0.65
                    text: qsTr(userModel.lastUser)
                    anchors.verticalCenter: parent.verticalCenter
                    font.pixelSize: height * 0.5
                    textColor: config.charcoal

                    KeyNavigation.backtab: rebootButton; KeyNavigation.tab: password

                    Keys.onPressed: {
                        if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) {
                            sddm.login(name.text, password.text, session.index)
                            event.accepted = true
                        }
                    }
                }
            }

            Row {
                width: parent.width
                spacing: Math.round(parent.width * 0.06)
                anchors.verticalCenter: greet.verticalCenter
                anchors.verticalCenterOffset: (mainRect.height * 0.30)
            
                Text {
                    id: lblPassword
                    width: parent.width * 0.25
                    color: config.periwinkle
                    text: qsTr(config.password)
                    font.pixelSize: 28
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideMiddle
                }

                PasswordBox {
                    id: password
                    width: parent.width * 0.65
                    font.pixelSize: height * 0.5
                    anchors.verticalCenter: parent.verticalCenter
                    tooltipBG: config.charcoal
                    textColor: config.charcoal
                    focus: true
                    Timer {
                        interval: 200
                        running: true
                        onTriggered: password.forceActiveFocus()
                    }

                    KeyNavigation.backtab: name; KeyNavigation.tab: session

                    Keys.onPressed: {
                        if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) {
                            sddm.login(name.text, password.text, session.index)
                            event.accepted = true
                        }
                    }
                }
            }

            Row {
                width: parent.width
                spacing: Math.round(parent.width * 0.06)
                anchors.verticalCenter: greet.verticalCenter
                anchors.verticalCenterOffset: (mainRect.height * 0.35)

                Text {
                    id: lblSession
                    width: parent.width * 0.25
                    color: config.periwinkle
                    text: qsTr(config.session)
                    font.pixelSize: 28
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideMiddle
                }

                ComboBox {
                    id: session
                    width: parent.width * 0.65
                    font.pixelSize: height * 0.5
                    anchors.verticalCenter: parent.verticalCenter

                    arrowIcon: "angle-down.png"

                    model: sessionModel
                    index: sessionModel.lastIndex

                    KeyNavigation.backtab: password; KeyNavigation.tab: loginButton
                }
            }

            Column {
                width: parent.width
                Text {
                    id: errorMessage
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: config.prompt
                    color: config.periwinkle
                    font.pixelSize: 28
                }
            }

            Row {
                width: parent.width
                spacing: Math.round(parent.width * 0.06)
                anchors.verticalCenter: greet.verticalCenter
                anchors.verticalCenterOffset: (mainRect.height * 0.55)
                anchors.horizontalCenter: greet.horizontalCenter
                anchors.horizontalCenterOffset: mainRect.width * 0.1

                Button {
                    id: loginButton
                    text: qsTr(config.login)
                    width: 128
                    height: 32
                    font.pixelSize: height * 0.5
                    color: config.periwinkle
                    textColor: config.charcoal
                    activeColor: config.grape
                    pressedColor: config.cadet
                    radius: 5

                    onClicked: sddm.login(name.text, password.text, session.index)

                    KeyNavigation.backtab: session; KeyNavigation.tab: shutdownButton
                }

                Button {
                    id: shutdownButton
                    text: qsTr(config.shutdown)
                    width: 128
                    height: 32
                    font.pixelSize: height * 0.5
                    color: config.periwinkle
                    textColor: config.charcoal
                    activeColor: config.grape
                    pressedColor: config.cadet
                    radius: 5

                    onClicked: sddm.powerOff()

                    KeyNavigation.backtab: loginButton; KeyNavigation.tab: rebootButton
                }

                Button {
                    id: rebootButton
                    text: qsTr(config.reboot)
                    width: 128
                    height: 32
                    font.pixelSize: height * 0.5
                    color: config.periwinkle
                    textColor: config.charcoal
                    activeColor: config.grape
                    pressedColor: config.cadet
                    radius: 5

                    onClicked: sddm.reboot()

                    KeyNavigation.backtab: shutdownButton; KeyNavigation.tab: name
                }
            }
        }
    }

    Component.onCompleted: {
        if (name.text == "")
            name.focus = true
        else
            password.focus = true
    }
}
